﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;

namespace ExtendedFilter.ExtendedFilter
{
	public class FamilyItemData : ItemData
	{
		public FamilyItemData(FamilyItemData rhs)
		{
			this.m_Name = rhs.m_Name;
			this.m_Selected = rhs.m_Selected;
			this.m_TypeItemList = rhs.m_TypeItemList;
			this.m_GroupingDataList = rhs.m_GroupingDataList;
			this.m_Properties = rhs.m_Properties;
		}

		public FamilyItemData(string name, List<TypeItemData> typeItemList, List<PropertyInfo> properties)
		{
			this.m_Name = name;
			this.m_Selected = true;
			this.m_TypeItemList = typeItemList;
			this.m_Properties = properties;
		}

		public string TheName
		{
			get
			{
				return this.m_Name;
			}
		}

		public bool Selected
		{
			get
			{
				return this.m_Selected;
			}
		}

		public List<TypeItemData> TypeItemList
		{
			get
			{
				return this.m_TypeItemList;
			}
		}

		public List<PropertyInfo> ThePropertyInfo
		{
			get
			{
				return this.m_Properties;
			}
		}

		public List<GroupingDataByProperty> GroupingDataList
		{
			get
			{
				return this.m_GroupingDataList;
			}
		}

		public int FindTypeItemIndex(string typeName)
		{
			return this.m_TypeItemList.FindIndex((TypeItemData value) => value.TheName.CompareTo(typeName) == 0);
		}

		public void AddElement(Document doc, Element ele)
		{
			Element element = doc.GetElement(ele.GetTypeId());
			string name = ele.Category.Name;
			if (element != null)
			{
				name = element.Name;
			}
			int num = this.FindTypeItemIndex(name);
			if (-1 == num)
			{
				List<Element> elements = new List<Element>();
				TypeItemData typeItemData = new TypeItemData(name, elements);
				typeItemData.AddElement(ele);
				this.m_TypeItemList.Add(typeItemData);
				return;
			}
			this.m_TypeItemList[num].AddElement(ele);
		}

		public void ResetGroupingDataList(List<GroupingDataByProperty> groupingDataList)
		{
			this.m_GroupingDataList = groupingDataList;
		}

		public void ChangePropertyInfo(int index, bool isChecked)
		{
			if (index < 0 || index > this.m_Properties.Count - 1)
			{
				return;
			}
			this.m_Properties[index].Checked = isChecked;
		}

		public override List<Element> GetSelectElements(FilterType filterType)
		{
			List<Element> list = new List<Element>();
			if (filterType == FilterType.FT_Type)
			{
				using (List<TypeItemData>.Enumerator enumerator = this.m_TypeItemList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						TypeItemData typeItemData = enumerator.Current;
						list.AddRange(typeItemData.GetSelectElements(filterType));
					}
					goto IL_9F;
				}
			}
			if (this.m_Selected && this.m_GroupingDataList.Count == 0)
			{
				return this.GetAllElements();
			}
			foreach (GroupingDataByProperty groupingDataByProperty in this.m_GroupingDataList)
			{
				list.AddRange(groupingDataByProperty.GetSelectElements(filterType));
			}
			IL_9F:
			return base.Unique(list);
		}

		public override List<Element> GetAllElements()
		{
			List<Element> list = new List<Element>();
			foreach (TypeItemData typeItemData in this.m_TypeItemList)
			{
				list.AddRange(typeItemData.GetAllElements());
			}
			return list;
		}

		public override void SetSelected(bool selected)
		{
			this.m_Selected = selected;
			foreach (TypeItemData typeItemData in this.m_TypeItemList)
			{
				typeItemData.SetSelected(selected);
			}
		}

		private string m_Name;

		private bool m_Selected = true;

		private List<TypeItemData> m_TypeItemList = new List<TypeItemData>();

		private List<GroupingDataByProperty> m_GroupingDataList = new List<GroupingDataByProperty>();

		private List<PropertyInfo> m_Properties;
	}
}
